﻿using System;
using System.Configuration;
using System.IO;
using ZilLion.Core.Log;
using ZilLion.Core.Unities.UnitiesMethods.WebApi.Entity;
using ZilLion.Core.Unities.UnitiesMethods.WebApi.Http;

namespace ZilLion.Task.Domain
{
    public class WebApiRepositoryBase<TClient> where TClient : IHttpClient, new()
    {
        /// <summary>
        ///     构造函数
        /// </summary>
        public WebApiRepositoryBase()
        {
            HttpClient = new TClient();
            DefaultIdentity = new RequestIdentity();
            HostUrl = ConfigurationManager.AppSettings["WebApiUrl"];
        }

        /// <summary>
        ///     客户端
        /// </summary>
        protected IHttpClient HttpClient { get; set; } //没有压缩

        /// <summary>
        ///     默认的请求参数
        /// </summary>
        protected RequestIdentity DefaultIdentity { get; set; }

        /// <summary>
        ///     配置的域名
        /// </summary>
        protected string HostUrl { get; set; }

        /// <summary>
        ///     Get方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="identity"></param>
        /// <returns></returns>
        public T HttpGet<T>(string url, RequestIdentity identity) where T : class, new()
        {
            T result;
            try
            {
                result = HttpClient.HttpGet<T>($@"{HostUrl}/{url}", identity);
            }
            catch (Exception ex)
            {
                ZilLionLogManager.GetLogger().Error(ex, tags: $@"{HostUrl}/{url}");
                return null;
            }
            return result;
        }

        /// <summary>
        ///     Get 方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <returns></returns>
        public T HttpGet<T>(string url) where T : class, new()
        {
            return HttpGet<T>(url, DefaultIdentity);
        }

        /// <summary>
        ///     Post方法
        /// </summary>
        /// <typeparam name="TP"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <param name="url"></param>
        /// <param name="identity"></param>
        /// <returns></returns>
        public T HttpPost<TP, T>(TP param, string url, RequestIdentity identity) where T : class, new()
        {
            T result;
            try
            {
                result = HttpClient.HttpPost<TP, T>(param, $@"{HostUrl}/{url}", identity);
            }
            catch (Exception ex)
            {
                ZilLionLogManager.GetLogger().Error(ex, tags: $@"{HostUrl}/{url}");
                return null;
            }
            return result;
        }

        /// <summary>
        ///     Post方法
        /// </summary>
        /// <typeparam name="TP"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public T HttpPost<TP, T>(TP param, string url) where T : class, new()
        {
            return HttpPost<TP, T>(param, url, DefaultIdentity);
        }

        /// <summary>
        ///     上传文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public T UpLoadFile<T>(string url, string path) where T : class, new()
        {
            return HttpClient.UpLoadFile<T>($@"{HostUrl}/{url}", path);
        }


        public bool DownloadFile(string url, string saveFilePath)
        {
            HttpHelper.DownloadFile(url, saveFilePath);
            if (File.Exists(saveFilePath))
                return true;
            return false;
        }
    }
}